package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiantian.bean.PageBean;
import com.tiantian.dao.BusinessUsersRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Businessusers;
import com.tiantian.service.BusinessUsersService;
import com.tiantian.util.CommonConstant.QueryType;
import com.tiantian.util.ConditionResult;

/**
 * @description 审核管理
 */
@Service
@Transactional
public class BusinessUsersServiceImpl extends GenericServiceImpl<Businessusers, String> implements BusinessUsersService {

	private BusinessUsersRepository repository;

	@Autowired
	public void setRepository(BusinessUsersRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	/**
	 * 获取商家用户列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 */
	public List<Businessusers> list(Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.LIST);
		List<Businessusers> ret = repository.queryListBySql(conditionResult.getSql(), pageBean.getStartIndex(), pageBean.getPageSize(),
				conditionResult.getParams(), Businessusers.class);
		conditionResult = getQueryCondition(params, QueryType.COUNT);
		pageBean.setTotal(repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams()));
		return ret;
	}

	/**
	 * 封装查询语句
	 * 
	 * @param searchMap
	 * @param type
	 * @return
	 */
	private ConditionResult getQueryCondition(Map<String, String> searchMap, QueryType type) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		switch (type) {
		case LIST:
			sql.append(" select t.* ");

			break;
		case COUNT:
			sql.append(" select count(1)");
			break;
		default:
			break;
		}
		sql.append(" from businessusers t where 1=1 ");

		if (searchMap != null) {

			// 商家生财ID
			String businessUserId = searchMap.get("businessUserId");
			if (StringUtils.isNoneBlank(businessUserId)) {
				sql.append(" and businessUserId like :businessUserId ");
				params.put("businessUserId", '%' + businessUserId.trim() + '%');
			}

			// 商家登陆账号
			String userName = searchMap.get("userName");
			if (StringUtils.isNoneBlank(userName)) {
				sql.append(" and userName like :userName ");
				params.put("userName", '%' + userName.trim() + '%');
			}

			// 账号状态
			String accountStatus = searchMap.get("accountStatus");
			if (StringUtils.isNoneBlank(accountStatus)) {
				sql.append(" and accountStatus=:accountStatus ");
				params.put("accountStatus", accountStatus.trim());
			}
			// 审核申请状态
			String phone = searchMap.get("phone");
			if (StringUtils.isNoneBlank(phone)) {
				sql.append(" and phone like :phone ");
				params.put("phone", '%' + phone.trim() + '%');
			}
			// 联系QQ
			String qqno = searchMap.get("qqno");
			if (StringUtils.isNoneBlank(qqno)) {
				sql.append(" and qqno like :qqno ");
				params.put("qqno", '%' + qqno.trim() + '%');
			}
			// 联系微信
			String wechatNo = searchMap.get("wechatNo");
			if (StringUtils.isNoneBlank(wechatNo)) {
				sql.append(" and wechatNo like :wechatNo ");
				params.put("wechatNo", '%' + wechatNo.trim() + '%');
			}
			// 店铺名字
			String storeName = searchMap.get("storeName");
			if (StringUtils.isNoneBlank(storeName)) {
				sql.append(" and storeName like :storeName ");
				params.put("storeName", '%' + storeName.trim() + '%');
			}

			// 开始时间
			String starttime = searchMap.get("starttime");
			if (StringUtils.isNoneBlank(starttime)) {
				sql.append(" and registrationTime>=:starttime ");
				params.put("starttime", starttime.trim());
			}
			// 结束时间
			String endtime = searchMap.get("endtime");
			if (StringUtils.isNoneBlank(endtime)) {
				sql.append(" and registrationTime<=:endtime ");
				params.put("endtime", endtime.trim());
			}
		}
		if (type == QueryType.LIST) {
			sql.append(" order by t.registrationTime desc");
		}
		return new ConditionResult(sql.toString(), params);

	}

	public List<Businessusers> getReportlist(Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.COUNT);
		pageBean.setTotal(repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams()));
		conditionResult = getQueryCondition(params, QueryType.LIST);
		List<Businessusers> ret = repository.queryListBySql(conditionResult.getSql(), 0, pageBean.getTotal().intValue(), conditionResult.getParams(),
				Businessusers.class);
		return ret;
	}
}
